/**
 *
 */
package org.jeecg.client;

import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * @author bright
 * 客户端访问接口
 */
public class ClientMng implements IBizCommand, Closeable {
	/**
	 * 客户端对象
	 */
	//public static ClientMng Instance;

	public static final ClientMng Instance = new ClientMng();
	protected List<IBizCommand> servers;
	protected IBizCommand currentServer;
	String userid;
	String password;
	String processId;
	String program;
	String lang;
	/**
	 * 构造函数
	 *
	 * @throws Exception
	 */
	public ClientMng() {
		System.out.println("创建对象！");
		if (Instance != null) {
			System.out.println("客户端已经创建！");
			try {
				throw new Exception("Client has created");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		try {
			this.Init();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	//静态工厂方法
	public static ClientMng getInstance() {
		return Instance;
	}

	protected void Init() throws Exception {
		System.out.println("初始化！");
		System.out.println(System.getProperty("user.dir"));
		String filePath = System.getProperty("user.dir") + "/resource/midw-client.properties";
		// 加载配置文件
		Properties prop = new Properties();
		InputStream is = null;
		 // 创建File对象  
        File file = new File(filePath);  
        if (file.exists()) {
        	is = new FileInputStream(filePath);
        } else {
        	is = ClientMng.class.getResourceAsStream("/midw-client.properties");
        }
		try {
			if (is == null)
				throw new IOException("Midware client config is not exists.");
			prop.load(is);
		} finally {
			if (is != null) {
				is.close();
			}
		}

		// 加载配置参数
		userid = prop.getProperty("userid");
		password = prop.getProperty("password");
		program = prop.getProperty("program");
		lang = prop.getProperty("locale");
		processId = "0";

		// 加载Server连接
		servers = new ArrayList<IBizCommand>();
		for (Map.Entry e : prop.entrySet()) {
			String key = (String) e.getKey();
			String value = (String) e.getValue();
			if (!key.startsWith("server."))
				continue;

			String name = key.substring("server.".length());
			servers.add(ClientFactory.CreateClient(name, value));
		}
		if (servers.size() == 0)
			throw new Exception("No server");

		currentServer = servers.get(0);
	}

	protected void Uninit(){

	}

	/**
	 * 切换服务器
	 */
	public void changeServer(){
		int pos = (servers.indexOf(currentServer) + 1)% servers.size();
		currentServer = servers.get(pos);
	}

	/**
	 * 创建request对象
	 * @param serverId
	 * @param clientId
	 * @param entityId
	 * @param cmdName
	 * @param parameters
	 * @return
	 */
	public BizRequest createRequest(String serverId, String clientId, String entityId, String cmdName, Map<String, String> parameters){
		BizRequest request = new BizRequest();
		request.setServerId(serverId);
		request.setClientId(clientId);
		request.setCmdName(cmdName);
		request.setEntityId(entityId);
		request.setParameters(parameters);
		request.setLang(lang);
		request.setUserId(userid);
		request.setPassword(password);
		request.setProcessId(processId);
		request.setProgram(program);
		return request;
	}

	/**
	 * 关闭客户端对象
	 */
	@Override
	public void close() throws IOException {
		Uninit();
		//Instance = null;
	}

	/**
	 * 执行命令
	 */
	@Override
	public BizResponse RunCmd(BizRequest request) {
		return currentServer.RunCmd(request);
	}

	/**
	 * 执行数据集命令
	 */
	@Override
	public DataSet RunDbCmd(BizRequest request) throws Exception {
		return currentServer.RunDbCmd(request);
	}
	
}
